<template>
    <div>

        <el-breadcrumb separator-class="el-icon-arrow-right"
                       style="background-color:white;padding: 8px;margin-bottom: 20px">
            <el-breadcrumb-item>首页</el-breadcrumb-item>
            <el-breadcrumb-item>航空航班管理</el-breadcrumb-item>
            <el-breadcrumb-item>增加航班信息</el-breadcrumb-item>
        </el-breadcrumb>


        <el-form label-width="100px" style="width: 500px;" size="small " ref="flightForm" :model="flight" :rules="rules">
            <el-form-item label="航班编号" prop="fliNo">
                <el-input v-model="flight.fliNo" placeHolder=""/>
            </el-form-item>


            <el-form-item label="飞机编号" prop="airCode">

                <el-select v-model="flight.airCode" placeholder="请选择飞机型号"
                           style="float: left;width: 400px">
                    <el-option
                            v-for="item in planeOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>


            </el-form-item>

            <el-form-item label="公司编号" prop="comCode">
                <el-select v-model="flight.comCode" placeholder="请选择公司编号" style="float: left;width: 400px">
                    <el-option
                            v-for="item in companyOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="起始地址" prop="fliBaddress">
                <el-select v-model="flight.fliBaddress" filterable placeholder="请选择" style="float: left;width: 400px">
                    <el-option
                            v-for="itemB in cityOptions"
                            :key="itemB.value"
                            :label="itemB.label"
                            :value="itemB.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="到达地址" prop="fliAaddress">
                <!--                这里有问题！不能和起始地址重复-->
                <el-select v-model="flight.fliAaddress" filterable placeholder="请选择" style="float: left;width: 400px">
                    <el-option
                            v-for="itemA in cityOptions"
                            :key="itemA.value"
                            :label="itemA.label"
                            :value="itemA.value">
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="起飞时间" prop="fliBtime">
                <el-date-picker
                        format="yyyy-MM-dd HH:mm"
                        value-format="yyyy-MM-dd HH:mm"
                        v-model="flight.fliBtime"
                        type="datetime"
                        placeholder="选择日期时间"
                        style="float: left;width: 400px">
                </el-date-picker>
            </el-form-item>

            <el-form-item label="到达时间" prop="fliAtime">
                <el-date-picker
                        format="yyyy-MM-dd HH:mm"
                        value-format="yyyy-MM-dd HH:mm"
                        v-model="flight.fliAtime"
                        type="datetime"
                        placeholder="选择日期时间"
                        style="float: left;width: 400px">
                </el-date-picker>
            </el-form-item>

            <el-row>
                <el-col :span="12">
                    <el-form-item label="头等舱价格" prop="fliFfare">
                        <el-input v-model="flight.fliFfare" placeHolder=""/>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="剩余人数" prop="fliFnumber">
                        <el-input v-model="flight.fliFnumber" placeHolder=""/>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="12">
                    <el-form-item label="公务舱价格" prop="fliCfare">
                        <el-input v-model="flight.fliCfare" placeHolder=""/>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="剩余人数" prop="fliCnumber">
                        <el-input v-model="flight.fliCnumber" placeHolder=""/>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="12">
                    <el-form-item label="经济舱价格" prop="fliYfare">
                        <el-input v-model="flight.fliYfare" placeHolder=""/>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="剩余人数" prop="fliYnumber">
                        <el-input v-model="flight.fliYnumber" placeHolder=""/>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-form-item>
                <el-row>
                    <el-col :span="12">
                        <el-button type="primary" @click="registry" :disabled="regBtnSts">立即创建</el-button>
                    </el-col>
                    <el-col :span="12">
                        <el-button @click="clear" size="medium">全部清空</el-button>
                    </el-col>
                </el-row>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        name: "AddAir",
        created() {
            this.findList();
        },
        methods: {

            findList() {
                axios.post("/flight/findListAddFlight", {}).then(resp => {
                    console.log(resp.data);

                    for (var i = 0; i < resp.data.length; i++) {
                        this.AddAirCode.push(resp.data[i].airCode);
                    }
                    this.planeOptions = this.AddAirCode.map(item => {
                        return {value: `${item}`, label: `${item}`};
                    });

                })
            },


            registry() {
                //将提交按钮设置为不可用状态 按钮动态加disable属性
                this.regBtnSts = true;
                //1、先验证表单格式没问题了，再允许发送请求
                this.$refs['flightForm'].validate((valid) => {
                    if (valid) {
                        //clone对象
                        let payLoad = _.clone(this.flight);
                        //删除payLoad的checkPass属性
                        // delete payLoad.checkPass;
                        // 进行表单的提交
                        axios.post("/flight/save", payLoad).then(response => {
                            this.regBtnSts = false;
                            this.clear();
                            if (response.data === 'success') {
                                this.$notify({
                                    title: "提示",
                                    message: "用户保存成功",
                                    type: "success"
                                })
                            }
                        })
                    } else {
                        this.regBtnSts = false;
                        return false;
                    }
                })
            },
            clear() {
                this.flight = {
                    fliNo: "",          //航班编号
                    airCode: "",        //飞机编号
                    comCode: "",        //公司编号
                    fliBaddress: '',    //起始地址
                    fliAaddress: '',     //到达地址
                    fliBtime: '',        //起飞时间
                    fliAtime: '',        //到达时间
                    fliFfare: '',       //头等舱价格
                    fliCfare: '',       //公务舱价格
                    fliYfare: '',       //经济舱价格
                    fliFnumber: '',     //头等舱剩余人数
                    fliCnumber: '',     //头等舱剩余人数
                    fliYnumber: '',     //头等舱剩余人数
                }
            }
        },
        data() {
            //航班编号不能为空，且长度在3-10之间
            let validFliNo = (rule, value, callback) => {
                if (!value || value === '') {
                    return callback(new Error("航班编号不能为空"));
                }
                if (value.length < 3 || value.length > 10) {
                    return callback(new Error("航班编号长度3-10之间"));
                }
                return callback();
            };
            //飞机编号不能为空
            let validAirCode = (rule, value, callback) => {
                if (!value || value === '') {
                    return callback(new Error("飞机编号不能为空"));
                }
                return callback();
            };
            //公司编号不能为空
            let validComCode = (rule, value, callback) => {
                if (!value || value === '') {
                    return callback(new Error("公司编号不能为空"));
                }
                return callback();
            };
            //起始地址不能为空
            let validFliBadDress = (rule, value, callback) => {
                if (!value || value === '') {
                    return callback(new Error("起始地址不能为空"));
                }
                return callback();
            };
            //到达地址不能为空，且不能和起始地址相同
            let validFliAadDress = (rule, value, callback) => {
                if (!value || value === '') {
                    return callback(new Error("到达地址不能为空"));
                }
                if(value===this.flight.fliBaddress){
                    return callback(new Error("到达地址不能和起始地址相同！"));
                }
                return callback();
            };
            //起飞时间不能为空，且大于当前时间
            let validFliBtime = (rule, value, callback) => {
                Date.prototype.Format = function (fmt) {
                    var o = {
                        "M+": this.getMonth() + 1, // 月份
                        "d+": this.getDate(), // 日
                        "h+": this.getHours(), // 小时
                        "m+": this.getMinutes(), // 分
                        "s+": this.getSeconds(), // 秒
                        "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
                        "S": this.getMilliseconds() // 毫秒
                    };
                    if (/(y+)/.test(fmt))
                        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + ""));
                    for (var k in o)
                        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                    return fmt;
                }
                if (!value || value === '') {
                    return callback(new Error("起飞时间不能为空"));
                }
                if(value<(new Date(new Date().getTime()).Format('yy-MM-dd hh:mm:ss'))){
                    return callback(new Error("起飞时间应大于当前时间"));
                }
                return callback();
            };
            //到达时间不能为空，且大于起飞时间
            let validFliAtime = (rule, value, callback) => {
                if (!value || value === '') {
                    return callback(new Error("到达时间不能为空"));
                }
                if(value<this.flight.fliBtime){
                    return callback(new Error("到达时间不能小于起飞时间！"));
                }
                return callback();
            };
            //头等舱价格不能为空，正则：不能以0开头，小数点最多两位
            let validFliFfare = (rule, value, callback) => {
                if (!value || value === '') {
                    return callback(new Error("头等舱价格不能为空"));
                }
                let pattern = /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/;
                if (!pattern.test(value)) {
                    return callback(new Error("格式错误！请重新输入"));
                }
                return callback();
            };
            //公务舱价格不能为空，正则：不能以0开头，小数点最多两位
            let validFliCfare = (rule, value, callback) => {
                if (!value || value === '') {
                    return callback(new Error("公务舱价格不能为空"));
                }
                let pattern = /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/;
                if (!pattern.test(value)) {
                    return callback(new Error("格式错误！请重新输入"));
                }
                return callback();
            };
            //经济舱价格不能为空，正则：不能以0开头，小数点最多两位
            let validFliYfare = (rule, value, callback) => {
                if (!value || value === '') {
                    return callback(new Error("经济舱价格不能为空"));
                }
                let pattern = /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/;
                if (!pattern.test(value)) {
                    return callback(new Error("格式错误！请重新输入"));
                }
                return callback();
            };
            //头等舱人数不能为空，正则：非负整数
            let validFliFnumber = (rule, value, callback) => {
                if (!value || value === '') {
                    return callback(new Error("头等舱价格不能为空"));
                }
                let pattern = /^[1-9]\d*|0$/;
                if (!pattern.test(value)) {
                    return callback(new Error("格式错误！请重新输入"));
                }
                return callback();
            };
            //公务舱人数不能为空，正则：非负整数
            let validFliCnumber = (rule, value, callback) => {
                if (!value || value === '') {
                    return callback(new Error("公务舱价格不能为空"));
                }
                let pattern = /^[1-9]\d*|0$/;
                if (!pattern.test(value)) {
                    return callback(new Error("格式错误！请重新输入"));
                }
                return callback();
            };
            //经济舱人数不能为空，正则：非负整数
            let validFliYnumber = (rule, value, callback) => {
                if (!value || value === '') {
                    return callback(new Error("经济价格不能为空"));
                }
                let pattern = /^[1-9]\d*|0$/;
                if (!pattern.test(value)) {
                    return callback(new Error("格式错误！请重新输入"));
                }
                return callback();
            };




            return {
                rules: {
                    fliNo: [
                        {validator: validFliNo, trigger: 'blur'}
                    ],
                    airCode: [
                        {validator: validAirCode, trigger: 'change'}
                    ],
                    comCode: [
                        {validator: validComCode, trigger: 'change'}
                    ],
                    fliBaddress: [
                        {validator: validFliBadDress, trigger: 'change'}
                    ],
                    fliAaddress: [
                        {validator: validFliAadDress, trigger: 'change'}
                    ],
                    fliBtime: [
                        {validator: validFliBtime, trigger: 'change'}
                    ],
                    fliAtime: [
                        {validator: validFliAtime, trigger: 'change'}
                    ],
                    fliFfare: [
                        {validator: validFliFfare, trigger: 'blur'}
                    ],
                    fliCfare: [
                        {validator: validFliCfare, trigger: 'blur'}
                    ],
                    fliYfare: [
                        {validator: validFliYfare, trigger: 'blur'}
                    ],
                    fliFnumber: [
                        {validator: validFliFnumber, trigger: 'blur'}
                    ],
                    fliCnumber: [
                        {validator: validFliCnumber, trigger: 'blur'}
                    ],
                    fliYnumber: [
                        {validator: validFliYnumber, trigger: 'blur'}
                    ],
                    // manAccount: [
                    //     {required: true, message: '用户名不能为空', trigger: 'blur'},
                    //     {min: 6, max: 20, message: '用户名6-20之间', trigger: 'blur'},
                    //     {validator: validAccount, trigger: 'blur'}
                    // ],
                    // checkPass: [
                    //     {validator: validPwd2, trigger: 'blur'}
                    // ],
                    // manId: [
                    //     {validator: validId, trigger: "blur"}
                    // ],
                    // manSex: [
                    //     {required: true, message: "请选择性别", trigger: "blur"}
                    // ],
                    // manTelNumber: [
                    //     {validator: validPhone, trigger: "blur"}
                    // ],
                    // manEmail: [
                    //     {validator: validEmail, trigger: "blur"}
                    // ]
                },
                //findAirCodeInfo对象实际是从后台查询得到的，代表了分页必要的信息


                regBtnSts: false,
                flight: {
                    fliNo: "",          //航班编号
                    airCode: "",        //飞机编号
                    comCode: "",        //公司编号
                    fliBaddress: '',    //起始地址
                    fliAaddress: '',     //到达地址
                    fliBtime: '',        //起飞时间
                    fliAtime: '',        //到达时间
                    fliFfare: '',       //头等舱价格
                    fliCfare: '',       //公务舱价格
                    fliYfare: '',       //经济舱价格
                    fliFnumber: '',     //头等舱剩余人数
                    fliCnumber: '',     //公务舱剩余人数
                    fliYnumber: '',     //经济舱剩余人数
                    flag: 0,
                    fliDiscount: 0,     //折扣
                    fliRefundtime: 0,  //退票记录时间
                },

                //用于：当后端给前端返回集合时，接收此集合
                AddAirCode: [],

                //接收来自AddAirCode的值
                planeOptions: [
                    // this.findAirCode()
                    // {
                    //     value: '飞机1',
                    //     label: '飞机1'
                    // }, {
                    //     value: '飞机2',
                    //     label: '飞机2'
                    // }
                ],
                // value: '',
                companyOptions: [{
                    value: '嘉晨航空',
                    label: '嘉晨航空'
                }, {
                    value: '新程航空',
                    label: '新程航空'
                }, {
                    value: '振宇航空',
                    label: '振宇航空'
                }, {
                    value: '毅泽航空',
                    label: '毅泽航空'
                }, {
                    value: '李达航空',
                    label: '李达航空'
                }, {
                    value: '国强航空',
                    label: '国强航空'
                }],
                cityOptions: [{
                    value: '北京',
                    label: '北京'
                }, {
                    value: '上海',
                    label: '上海'
                }, {
                    value: '广州',
                    label: '广州'
                }, {
                    value: '深圳',
                    label: '深圳'
                }, {
                    value: '苏州',
                    label: '苏州'
                }, {
                    value: '成都',
                    label: '成都'
                }, {
                    value: '天津',
                    label: '天津'
                }, {
                    value: '武汉',
                    label: '武汉'
                }, {
                    value: '南京',
                    label: '南京'
                }, {
                    value: '长沙',
                    label: '长沙'
                }, {
                    value: '郑州',
                    label: '郑州'
                }, {
                    value: '青岛',
                    label: '青岛'
                }, {
                    value: '无锡',
                    label: '无锡'
                }, {
                    value: '大连',
                    label: '大连'
                }, {
                    value: '西安',
                    label: '西安'
                }, {
                    value: '宁波',
                    label: '宁波'
                }, {
                    value: '济南',
                    label: '济南'
                }, {
                    value: '佛山',
                    label: '佛山'
                }, {
                    value: '重庆',
                    label: '重庆'
                },],
                fliNoValue: '',//航班编号绑定
                comNoValue: '',//公司编号绑定
                pickerOptions: {
                    shortcuts: [{
                        text: '今天',
                        onClick(picker) {
                            picker.$emit('pick', new Date());
                        }
                    }, {
                        text: '昨天',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24);
                            picker.$emit('pick', date);
                        }
                    }, {
                        text: '一周前',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', date);
                        }
                    }]
                },
            };
        }
    }
</script>

<style scoped>

</style>
